package xkdaima;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;



 public class SCThread extends Thread{//选课控制线程
    boolean scopen=true;//选课开启，为false时不再重复修改
    boolean scclose=false;//选课结束，为true时结束线程
    boolean comparetrue=true;//结束时间大于开始时间正确
    String starttime,endtime;

    public SCThread(String starttime,String endtime){
        super();
        this.starttime=starttime;
        this.endtime=endtime;
    }
    public SCThread() {
		
	}

    public void run() {
        while (true) {
            try {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                Calendar calendar = Calendar.getInstance();
                Date date = calendar.getTime();//获取系统时间
                String systemtime = sdf.format(date);
                CourseMaintain(systemtime,starttime,endtime);
                if(scclose) break;
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void CourseMaintain(String systemtime, String starttime, String endtime){//控制选课开放时段(定时),系统时间（systemtime），开始时间（starttime），结束时间（endtime）
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:source/xuanke.sqlite");
            SimpleDateFormat sformat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            Date startdate=new Date();
            startdate=sformat.parse(starttime);
            Date enddate=new Date();
            enddate=sformat.parse(endtime);
            Date systemdate=new Date();
            systemdate=sformat.parse(systemtime);
            if(((systemdate.compareTo(startdate))==0||(systemdate.compareTo(startdate))>0)&&scopen)
            {
                PreparedStatement pst=connection.prepareStatement("update CTL set IfSCourse=1");
                pst.executeUpdate();
                //System.out.println("选课开始!");
                pst.close();
                scopen=false;
            }
            else if((systemdate.compareTo(enddate))==0||(systemdate.compareTo(enddate))>0){
                PreparedStatement pst=connection.prepareStatement("update CTL set IfSCourse=0");
                pst.executeUpdate();
                //System.out.println("选课结束!");
                pst.close();
                scclose=true;
            }
            connection .close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void CourseSelectopen(){//开始选课(点击)
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:source/xuanke.sqlite");
            PreparedStatement pst=connection.prepareStatement("update CTL set IfSCourse=1");
            pst.executeUpdate();
            pst.close();
            connection .close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void CourseSelectclose(){//结束选课(点击)
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:source/xuanke.sqlite");
            PreparedStatement pst=connection.prepareStatement("update CTL set IfSCourse=0");
            pst.executeUpdate();
            pst.close();
            connection .close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean comparedate(String starttime, String endtime){//比较日期大小(当starttime<endtime,return true;即结束时间大于开始时间正确)
        try {
            SimpleDateFormat sformat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            Date startdate=new Date();
            startdate=sformat.parse(starttime);
            Date enddate=new Date();
            enddate=sformat.parse(endtime);
            if((enddate.compareTo(startdate))>0)
            {
                comparetrue=true;
            }
            else{
                comparetrue=false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return comparetrue;
    }


    public boolean judgedate(String str){//判断日期字符串格式是否正确(当格式正确,return true;)
        try {
            SimpleDateFormat sformat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            sformat.setLenient(false);
            sformat.parse(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    public String getsystemtime() {
    	  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
          Calendar calendar = Calendar.getInstance();
          Date date = calendar.getTime();
          String systemtime = sdf.format(date);
          return systemtime;
	}
}



